/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.thingsgrid.device.controller;
import com.thingsgrid.device.entity.Function;
import com.thingsgrid.device.entity.Product;
import com.thingsgrid.device.entity.ProductType;
import com.thingsgrid.device.entity.Topic;
import com.thingsgrid.device.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

/**
 *  控制器
 *
 * @author BladeX
 * @since 2019-08-19
 */
@RestController
//@AllArgsConstructor
@RequestMapping("/product")
@Api(value = "", tags = "接口")
public class ProductController{
	@Autowired
	private IProductService productService;
	/**
	 * 新增
	 */
	@PostMapping("/submit")
	@ApiOperation(value = "新增", notes = "传入product", position = 1)
	public R save(@Valid @RequestBody Product product) {
		return R.status(productService.saveProduct(product));
	}


	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "传入product", position = 3)
	public R update(@Valid @RequestBody Product product) {
		return R.status(productService.updateProduct(product));
	}

	/**
	 * 查找所有
	 */
	@GetMapping("/selectAll")
	@ApiOperation(value = "查询所有", notes = "", position = 4)
	public List<Product> selectAll() {
		return productService.findAll();
	}




	/**
	 * 根据id查信息
	 */
	@GetMapping("/detail")
	@ApiOperation(value = "查询一个", notes = "", position = 5)
	public R<Product> selectById(@ApiParam(value = "productId", required = true) @RequestParam String productId) {
		return R.data(productService.findProductById(productId));
	}

	/**
	 * 产品列表
	 */
	@GetMapping("/list")
	@ApiOperation(value = "列表", notes = "传入account和realName", position = 2)
	public R<HashMap<String, Object>> pageList(@ApiParam(value = "页数和条数", required = true)
									 @RequestParam Integer current, @RequestParam Integer size,
											   @RequestParam String productName,@RequestParam String productTypeId, @RequestParam String startTime,
											   @RequestParam String endTime,@RequestParam Integer isPublish){
		return R.data(productService.findAll(current,size,productName,productTypeId,startTime,endTime,isPublish));

	}

	/**
	 * 09.06
	 * @param productId
	 * @return
	 */
	@GetMapping("/listTopic")
	public R<List<Topic>> listTopicItems(@ApiParam(value = "产品id" ,required = true)
										 @RequestParam String productId){
		return R.data(productService.listTopicItems(productId));

	}

	/**
	 * 查询产品类型
	 */
	@GetMapping("/getProductType")
	public R<List<ProductType>> getProductType(){
		return R.data(productService.getProductType());

	}

	@GetMapping("/listSubscribeTopicItems")
	public R<List<Topic>> listSubscribeTopicItems(@ApiParam(value = "产品id" ,required = true)
												  @RequestParam String productId){
		return R.data(productService.listSubscribeTopicItems(productId));

	}


	@GetMapping("/getPhysicalId")
	public List<Function> getPhysicalId(@ApiParam(value = "传入产品id" ,required = true)
										@RequestParam String productId){
		productService.getPhysicalId(productId);
		return null;
	}
	@PostMapping("/addPhysical")
	public void addPhysical(@ApiParam(value = "产品id和物模型id" ,required = true)
							@RequestParam String productId, String physicalModelId){
		productService.addPhysical(productId,physicalModelId);
		//return(productService.getFunctionbyPhysicalModelId(physicalModelId));
	}
	@PostMapping("/removePhysical")
	public void removePhysical(@ApiParam(value = "产品id和物模型id" ,required = true)
							   @RequestParam String productId, String physicalModelId){
		productService.removePhysical(productId,physicalModelId);
	}
	@GetMapping("/listFunctionByProductId")
	public R<List<Function>> listFunctionByProductId(@ApiParam(value = "产品id和物模型id" ,required = true)
													 @RequestParam String productId){
		return R.data(productService.getFunctionbyPhysicalModelId  (productService.getPhysicalId(productId)));
	}

	@PostMapping("/addTopicItem")
	public void addTopicItem(@ApiParam(value = "产品id和topic类名",required=true)
							 @RequestParam String productId, String topicId){
		productService.addTopicItem(productId,topicId);
	}
	/**
	 * 删除
	 */
	@PostMapping("/removeTopicItem")
	@ApiOperation(value = "逻辑删除", notes = "传入ids", position = 2)
	public void removeTopicItem(@ApiParam(value = "主键集合", required = true)
								@RequestParam String productId, String topicId) {
		productService.removeTopicItem(productId,topicId);
	}
	//	@PostMapping("/addTopicItems")
//	public void addTopicItems(@ApiParam(value = "产品id和topic类名",required=true)
//							 @RequestParam  List<String> topicIds){
//		for (String topicId : topicIds) {
//			productService.addTopicItem(productId,topicId);
//		}
//
//	}
	@PostMapping("/addSubscribeTopicItem")
	public void addSubscribeTopicItems(@ApiParam(value = "产品id和topic类名",required=true)
									   @RequestParam String productId, String topicId){
		productService.addSubscribeTopicItem(productId,topicId);

	}

	@PostMapping("/removeSubscribeTopicItem")
	public void removeSubscribeTopicItems(@ApiParam(value = "产品id和topic类名",required=true)
										  @RequestParam String productId, String topicId){
		productService.removeSubscribeTopicItem(productId,topicId);

	}
//	public R<IPage<Product>> list(@ApiIgnore @RequestParam Map<String, Object> product, Query query, BladeProduct bladeUser) {
//		QueryWrapper<User> queryWrapper = Condition.getQueryWrapper(user, User.class);
//		IPage<User> pages = userService.page(Condition.getPage(query), (!bladeUser.getTenantCode().equals(BladeConstant.ADMIN_TENANT_CODE)) ? queryWrapper.lambda().eq(User::getTenantCode, bladeUser.getTenantCode()) : queryWrapper);
//		UserWrapper userWrapper = new UserWrapper(userService, dictClient);
//		return R.data(userWrapper.pageVO(pages));
//	}

	/**
	 * 可变参数查询
	 */
	@GetMapping("/selectByCondition")
	@ApiOperation(value = "根据条件查",notes = "可变条件",position = 7)
	public List<Product> selectByCondition(@Valid @RequestParam String productName, String productType, Timestamp beginTime, Timestamp endTime,
                                           Integer isPublish){
		return productService.selectByCondition(productName, productType, beginTime, endTime,
			isPublish);
	}

	/**
	 * 2019.08.28
	 * 多表联查
	 */
	@GetMapping("/findProductAndTopic")
	public Object findProductAndTopic(){
		return productService.findProductAndTopic();
	}








	//private IDictClient dictClient;\
//	/**
//	* 详情
//	*/
//	@GetMapping("/detail")
//	@ApiOperation(value = "详情", notes = "传入product", position = 1)
//	public R<ProductVO> detail(Product product) {
//		Product detail = productService.getOne(Condition.getQueryWrapper(product));
//		ProductWrapper productWrapper = new ProductWrapper(dictClient);
//		return R.data(productWrapper.entityVO(detail));
//	}
//
//	/**
//	* 分页
//	*/
//	@GetMapping("/list")
//	@ApiOperation(value = "分页", notes = "传入product", position = 2)
//	public R<IPage<ProductVO>> list(Product product, Query query) {
//		IPage<Product> pages = productService.page(Condition.getPage(query), Condition.getQueryWrapper(product));
//		ProductWrapper productWrapper = new ProductWrapper(dictClient);
//		return R.data(productWrapper.pageVO(pages));
//	}
//
//	/**
//	* 自定义分页
//	*/
//	@GetMapping("/page")
//	@ApiOperation(value = "分页", notes = "传入product", position = 3)
//	public R<IPage<ProductVO>> page(ProductVO product, Query query) {
//		IPage<ProductVO> pages = productService.selectProductPage(Condition.getPage(query), product);
//		return R.data(pages);
//	}



//	/**
//	* 修改
//	*/
//	@PostMapping("/update")
//	@ApiOperation(value = "修改", notes = "传入product", position = 5)
//	public R update(@Valid @RequestBody Product product) {
//		return R.status(productService.updateById(product));
//	}
//
//	/**
//	* 新增或修改
//	*/
//	@PostMapping("/submit")
//	@ApiOperation(value = "新增或修改", notes = "传入product", position = 6)
//	public R submit(@Valid @RequestBody Product product) {
//		return R.status(productService.saveOrUpdate(product));
//	}
//
//

	/**
	 *说明：取消产品发布或删除产品
	 *@author 陈学栋
	 *@since 2019/9/20 15:22
	 */
	@PostMapping("cancelPublishOrDelete")
	@ApiOperation(value = "取消产品发布和删除产品",notes = "传入产品ID",position = 8)
	public R cancelPublishOrDelete(@RequestParam String productId){
		long publish = productService.cancelPublishOrDelete(productId);
		if(publish != 0){
			return R.status(true);
		}else{
			return R.status(false);
		}
	}

	/**
	 * 说明：产品发布
	 * @author 陈学栋
	 * @since 2019/10/11 16:17
	 */
	@PostMapping("productPublish")
	@ApiOperation(value = "产品发布",notes = "传入产品ID",position = 8)
	public R productPublish(@RequestParam String productId){
		long publish = productService.productPublish(productId);
		if(publish != 0){
			return R.status(true);
		}else{
			return R.status(false);
		}
	}


	/**
	 * 说明：添加产品的Topic
	 * @author 陈学栋
	 * @since 2019/10/12 10:38
	 * @return
	 */
	@PostMapping("addProductTopic")
	@ApiOperation(value = "添加产品的Topic",notes = "传入产品ID和topic对象",position = 8)
	public boolean addProductTopic(@RequestParam String productId, @RequestBody Topic[] topic){
		return productService.addProductTopic(productId, topic);
	}


	/**
	 * 说明：删除产品的Topic
	 * @author 陈学栋
	 * @since 2019/10/12 10:38
	 * @return
	 */
	@PostMapping("removeProductTopic")
	@ApiOperation(value = "删除产品的Topic",notes = "传入产品ID和topic对象",position = 9)
	public R removeProductTopic(@RequestParam String productId, @RequestBody Topic topic){
		productService.removeProductTopic(productId, topic);
		return R.status(true);
	}
}
